<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>

<body>
  <div style="margin: 20px;">
    <span class="layui-breadcrumb">
      <a href="/home">首页</a>
      <a><cite>分类管理</cite></a>
    </span>
    <div id="add" style="margin-top: 10px">
      <a href="/cateAdd" class="layui-btn layui-btn-warm">增加分类</a>
    </div>

    <table class="layui-table">
      <thead>
        <tr>
          <th>分类名称</th>
          <th>分类图标</th>
          <th>分类描述</th>
          <th>操作</th>
        </tr> 
      </thead>
      <tbody>
        <% for(var i=0;i<cates.length;i++) { %>
          <tr>
            <td><%= cates[i].cateName %></td>
            <td>
              <img width="50" src="<%= cates[i].cateIcon || 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffile02.sg560.com%2Fupimg02%2F2014%2F12%2F07%2F12352903787772.jpg&refer=http%3A%2F%2Ffile02.sg560.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618213148&t=833ce0c9147f590e3bd61a7f41ef9499' %>" alt="">
            </td>
            <td><%= cates[i].cateDesc || '暂无描述' %></td>
            <td>
              <a href="/cateEdit?id=<%= cates[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
              <button data-id="<%= cates[i]._id %>" class="layui-btn layui-btn-danger del layui-btn-xs">删除</button>
            </td>
          </tr>
        <% } %>
        
      </tbody>
    </table>

    <div id="page"></div>
  </div>
  <script src="/static/lib/layui/layui.js"></script>
  <script>
    layui.use(['element','laypage', 'jquery', 'layer'], function () {
      var element = layui.element;
      var laypage = layui.laypage;
      var $ = layui.jquery;
      var layer = layui.layer;
      // 删除分类
      $('.del').click(function(){
        const _id = this.dataset.id;
        layer.confirm('确认删除吗?',{
          icon: 3, title:'删除分类'
        },function(index){
          // 发送ajax
          $.ajax('/delCate',{
            type: 'POST',
            data: {
              _id
            },
            success: function(res){
              if(res.code === 0 ){
                layer.msg(res.msg,{code:1,time:2000},function(){
                  location.reload();
                })
              }
            }
          })
        })
      })

      //执行一个laypage实例
      laypage.render({
        elem: 'page' // 直接写id
        ,curr:<%= current %>
        ,count: <%= counts %> //数据总数，从服务端得到
        ,limit: 2
        ,jump: function(obj, first){
          //obj包含了当前分页的所有参数，比如：
          console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
          console.log(obj.limit); //得到每页显示的条数
         
          //首次不执行
          if(!first){
            // do something
            location.href="/cateList?current="+obj.curr+"&pageSize="+obj.limit;
          }
        }
      });
    })
  </script>
</body>

</html>